package com.manager.controller;

import com.manager.bean.Valuable;
import com.manager.service.ValuableService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

import com.manager.bean.PageInfo;

/**
 * (Valuable)表控制层
 *
 * @author makejava
 * @since 2021-03-02 20:15:30
 */
@Controller
public class ValuableController {
    /**
     * 服务对象
     */
    @Resource
    private ValuableService valuableService;

    @RequestMapping(value = "/findValuable")
    public String findValuable(String name, String phone , Integer pageIndex,
                              Integer pageSize, Model model) {

        PageInfo<Valuable> pi = valuableService.findPageInfo(name,phone,
                pageIndex,pageSize);
        model.addAttribute("pi",pi);
        model.addAttribute("name",name);
        return "valuable_list";
    }



    @RequestMapping(value = "/exportvaluablelist", method = RequestMethod.POST)
    @ResponseBody
    public List<Valuable> exportValuable() {
        List<Valuable> valuableList = valuableService.getAll();
        return valuableList;
    }


    @RequestMapping("/deleteValuable")
    @ResponseBody
    public String deleteValuable(Integer Id) {
        valuableService.deleteById(Id);
        return "valuable_list";
    }


    @RequestMapping(value = "/addValuable", method = RequestMethod.POST)
    @ResponseBody
    public String addValuable(@RequestBody Valuable valuable) {
        valuable.setTime(new Date(new Date().getTime()));
        valuableService.insert(valuable);
        return "valuable_list";
    }


    @RequestMapping(value = "/updateValuable", method = RequestMethod.POST)
    public String updateValuable(Valuable valuable) {
        valuableService.update(valuable);
        return "redirect:/findValuable";
    }


    @RequestMapping("/findValuableById")
    public String findValuableById(Integer Id, HttpSession session, Model model) {
        Valuable s = valuableService.queryById(Id);
        session.setAttribute("s", s);
        model.addAttribute("s", s);
        return "valuable_edit";
    }

}
